import React, { Component, Fragment } from "react";
// antd
import { Table, Button, Col, Pagination, Form, Input, Space, Tag, Typography, Select, message, Modal } from "antd";
import Action from "@c/buttonAction/Index";

// http
import http from '../../../utils/request';

const { Text, Link } = Typography;
const { Option } = Select;

function ListItem(props) {
    return <div><Tag style={{ marginTop: "5px"}}>{props.value}</Tag></div>
}


class CloudAlarmThreshold extends Component {
    constructor(props){
        super(props);
        this.state = {
            // 表格分页
            total: '',
            page: 1,
            page_number: 10,
            dataSource: [],
            columns: [
                {title: "实例ID/名称", dataIndex: "instance_id", key: "instance_id",
                    render: (text, rowData) => {
                        return(
                            <Space direction="vertical">
                                <Link href="">
                                    {rowData.instance_id}
                                </Link>
                                <Text >{rowData.name}
                                </Text>
                            </Space>
                        )
                    }
                },
                {title: "云平台", dataIndex: "cloud-platform", key: "cloud-platform",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.platform}-{rowData.account_name}</Tag>                                
                            </div>
                        )
                    }
                },
                {title: "资源类型", dataIndex: "resource_type", key: "resource_type",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="gold">{rowData.resource_type}</Tag>
                            </div>
                        )
                    }
                },

                {title: "维护指标", dataIndex: "maintain_data", key: "maintain_data",
                    render: (text, rowData) => {
                        const listItems = rowData.maintain_data.map((item) => 
                                <ListItem key={item.toString()} value={item} />
                        );
                        return (
                            <ul>{listItems}</ul>
                        )
                    }
                },
                {title: "维护时间", dataIndex: "maintain_time", key: "maintain_time",
                    render: (text, rowData) => {
                        const listItems = rowData.maintain_time.map((item) => 
                                <ListItem key={item.toString()} value={item} />
                        );
                        return (
                            <ul>{listItems}</ul>
                        )
                    }
                },
                {title: "操作", dataIndex: "operation", key: "operation", 
                    render: (text, rowData) => {
                        return (
                            <div className="inline-button">
                                <Action>
                                    <Action.Button onClick={() => this.onDeleteCloudAlarmCallbackMaintainBtn(rowData)}>删除</Action.Button>
                                </Action>
                            </div>

                        )
                    }           
                }

            ],
            loading: false,
            visible_delete: false,
            search_instance_id: null,
            search_instance_name: null,
            search_instance_account_name: null,
            search_instance_platform: null,
            search_option_business: null,
            search_instance_maintain_data: null,
            search_option_platform: [],
            search_option_account: [],
        }
    }

    componentDidMount() {
        this.loadData()
        this.loadDataPlatform()
    }

    loadDataPlatform = () => {
        const requestData = {
            'page': 1,
            'limit': 1000,
        }
        http.get('/cloud/platform', {params: requestData}).then(response=>{
            const search_option_platform = []
            const resp_data = response.data.data.items
            for (let i = 0; i < resp_data.length; i++) {
                search_option_platform.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)
            }
            this.setState({
                search_option_platform: search_option_platform
            })
        })
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'instance_id__contains': this.state.search_instance_id,
            'name__contains': this.state.search_instance_name,
            'maintain_data__contains': this.state.search_instance_maintain_data,
            'platform': this.state.search_instance_platform,
            'account_name': this.state.search_instance_account_name,
        }

        // 获取分组列表
        http.get('/alarm/maintain', {params: requestData}).then(response=>{
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    /** 切换每页数量 */
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }

    /** 云平台搜索 */
    onFinishSearch = (data) => {
        this.setState({
            page: 1,
            search_instance_id: data.instance_id ? data.instance_id.trim() : null,
            search_instance_name: data.instance_name ? data.instance_name.trim() : null,
            search_instance_maintain_data: data.maintain_data ? data.maintain_data.trim() : null,
            search_instance_platform: data.platform ? data.platform : null,
        })
        this.loadData()
    }


    // 搜索切换业务线
    onChangeBusiness = (value) => {
        if (value) {
            this.setState({
                search_business_name: value
            })
        } else {
            this.setState({
                search_business_name: null,
            })
        }
    }

    // 搜索切换云平台
    onChangePlatform = (value) => {
        if (value) {
            this.setState({
                search_instance_platform: value
            })

            const requestData = {
                'page': 1,
                'limit': 1000,
                'platform': value
            }
    
            http.get('/cloud/account', {params: requestData}).then(response=>{
                const search_option_account = []
                const resp_data = response.data.data.items
                for (let i = 0; i < resp_data.length; i++) {
                    search_option_account.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)
                }
                this.setState({
                    search_option_account: search_option_account
                })
            })

        } else {
            this.setState({
                search_option_account: '',
            })
        }
    }

    // 搜索云账户
    onChangeAccountName = (value) => {
        this.setState({
            search_instance_account_name: value ? value : null
        })
    }

    /** 删除维护记录按钮 */
    onDeleteCloudAlarmCallbackMaintainBtn = (data) => {
        this.setState({
            cloud_alarm_callback_maintain_id: data.id,
            visible_delete: true
        })
    }

    /** 删除云平台提交 */
    handleCloudAlarmCallbackMaintainDelete = () => {
        http.delete('/cloud/maintain/' + this.state.cloud_alarm_callback_maintain_id).then(response=>{
            message.success(response.data.msg)
            this.setState({
                visible_delete: false,
            })
            this.loadData()
        }).catch()    
    }

    render(){
        const { loading, dataSource, columns, page, total, search_option_platform, search_option_account, visible_delete } = this.state
        const rowSelection = {
            onChange: this.onCheckbox
        }
        var myStyle = {margin: '20px'}
        var myStyle1 = {margin: '10px', float: 'right'}
        return (
            <Fragment>
                <Form layout="inline" onFinish={this.onFinishSearch}>
                    <Form.Item label="实例ID" name="instance_id">
                        <Input placeholder="请输入实例ID"  />
                    </Form.Item>
                    <Form.Item label="实例名称" name="instance_name">
                        <Input placeholder="请输入实例名称"  />
                    </Form.Item>
                    <Form.Item label="云平台" name="platform">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 180 }}
                            placeholder="请选择区域"
                            optionFilterProp="children"
                            onChange={this.onChangePlatform}
                            filterOption={(input, option) =>
                                option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                            }
                        >
                            {search_option_platform}
                        </Select>
                    </Form.Item>

                    <Form.Item label="云账户" name="account_name">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 180 }}
                            placeholder="请选择云账户"
                            onChange={this.onChangeAccountName}
                            >
                            {search_option_account}
                        </Select>                      
                    </Form.Item>

                    <Form.Item label="告警指标" name="maintain_data">
                        <Input placeholder="请输入指标信息"  />
                    </Form.Item>

                    <Form.Item shouldUpdate={true}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>
                
                <div style={myStyle1}><Button type="primary" onClick={this.onAddCloudPlatformBtn}></Button></div>
                <Table
                    pagination={false} 
                    rowSelection={rowSelection} 
                    rowKey="id" 
                    columns={columns} 
                    dataSource={dataSource}
                    loading={loading}
                    scroll={{ y: 500, x: '70vw' }}
                >
                </Table>

                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>

                <Modal
                    title="删除维护记录"
                    visible={visible_delete}
                    onOk={this.handleCloudAlarmCallbackMaintainDelete}
                    onCancel={() => {this.setState({visible_delete: false, cloud_alarm_callback_maintain_id: ''})}}
                    okText="确认"
                    cancelText="取消"
                >
                    <p>确定删除<span style={{color: 'red'}}>维护记录</span>? 删除后将无法恢复</p>
                </Modal>

            </Fragment>
        )
    }
}
export default CloudAlarmThreshold;